#pragma once
#include <iostream>
using namespace std;
#include <assert.h>

namespace my_std
{
	template <class T>
	struct __list_node
	{
		__list_node<T>* _prev;
		__list_node<T>* _next;
		T _val;

		__list_node(const T& val = T())
			:_prev(nullptr)
			,_next(nullptr)
			,_val(val)
		{}
	};


	template <class T,class ref,class prt>
	struct _list_iterator
	{
		typedef __list_node<T> Node;
		typedef _list_iterator<T, ref, prt> LLT;
		Node* _node;

		_list_iterator(Node* node)
			:_node(node)
		{}
		
		ref operator*()
		{
			return _node->_val;
		}

		prt operator->()
		{
			return &(operator*());
		}

		LLT& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		LLT operator++(int)
		{
			LLT tmp = *this;
			_node = _node->_next;
			return tmp;
		}

		LLT& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		LLT operator--(int)
		{
			LLT tmp = *this;
			_node = _node->_prev;
			return tmp;
		}

		bool operator!=(const LLT& lt)const
		{
			return _node != lt._node;
		}

		bool operator==(const LLT& lt)const
		{
			return _node == lt._node;
		}

	};
	template <class iterator, class ref, class prt>
	struct _reverse_iterator
	{
		typedef _reverse_iterator<iterator, ref, prt> self;
		iterator _it;

		_reverse_iterator(iterator it)
			:_it(it)
		{}

		ref operator*()
		{
			iterator tmp = _it;
			return *(--tmp);
		}

		prt operator->()
		{
			return &(operator*());
		}

		self& operator++()
		{
			--_it;
			return *this;
		}

		self operator++(int)
		{
			iterator tmp = _it--;
			return self(tmp);
		}


		self& operator--()
		{
			++_it;
			return *this;
		}

		self operator--(int)
		{
			iterator tmp = _it++;
			return self(tmp);
		}

		bool operator!=(const self& lt)const
		{
			return _it != lt._it;
		}

		bool operator==(const self& lt)const
		{
			return _it == lt._it;
		}

	};



	template <class T>
	class list
	{
		typedef __list_node<T> Node;
		Node* _head;
		size_t _size;
	public:
		typedef _list_iterator<T,T&, T*> iterator;
		typedef _list_iterator<T,const T&,const T*> const_iterator;

		typedef _reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef _reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
		
		void _init()
		{
			_head = new Node(T());
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list()
		{
			_init();
		}

		~list()
		{
			clear();
			delete _head;
		}

		void clear()
		{
			iterator it = begin();
			while (!empty())
			{
				it = erase(it);
			}
			_size = 0;
		}

		bool empty()
		{
			return begin() == end();
		}

		list(const list<T>& lt)
		{
			_init();
			for (auto & e : lt)
			{
				push_back(e);
			}
		}
		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}


		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		iterator begin()
		{
			return _head->_next;
		}

		iterator end()
		{
			return _head;
		}

		const_iterator begin()const
		{
			return _head->_next;
		}

		const_iterator end()const
		{
			return _head;
		}

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

		const_reverse_iterator rbegin()const
		{
			return const_reverse_iterator(begin());
		}

		const_reverse_iterator rend()const
		{
			return const_reverse_iterator(end());
		}


		size_t& size()
		{
			return _size;
		}


		void push_back(const T& val)
		{
			insert(end(), val);

		}
		void push_front(const T& val)
		{
			insert(begin(), val);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}


		iterator insert(const iterator& pos, const T& val)
		{
			
			Node* newnode = new Node(val);
			Node* tmp = pos._node->_prev;
			tmp->_next = newnode;
			newnode->_prev = tmp;
			pos._node->_prev = newnode;
			newnode->_next = pos._node;
			++_size;
			return newnode;
		}

		iterator erase(const iterator& pos)
		{
			assert(pos != end());

			Node* prev = pos._node->_prev;
			Node* next = pos._node->_next;

			prev->_next = next;
			next->_prev = prev;
			delete pos._node;
			--_size;
			return next;
		}
	};








};


